<template>
  <div>
    <!-- 列表 -->
    <!-- 
          data:数据
          row-key：一行的唯一标识
          border：加边框
           default-expand-all:默认展开所有数据
          default-expand-all：默认展开所有数据
           :tree-props="{ children: '由哪个字段来判断是否有子节点' }"
    -->
    <el-table
      :data="list"
      style="width: 100%"
      row-key="id"
      border
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="id" label="角色编号" sortable width="180"></el-table-column>
      <el-table-column prop="rolename" label="角色名称" sortable width="180"></el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-button type="primary" @click="handleEdit(scope)" v-if="scope.row.status == 1">启用</el-button>
          <el-button type="info" v-else>禁用</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click="handleEdit(scope)">编辑</el-button>
          <el-button type="danger" @click="handleDelete(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { reqroledelete } from "../../../http/api";
import { successAlert } from "../../../utils/alert";
export default {
  props: ["list"],
  data() {
    return {};
  },
  methods: {
    load(tree, treeNode, resolve) {
      setTimeout(() => {
        resolve([
          {
            id: 31,
            date: "2016-05-01",
            name: "王小虎",
            address: "上海市普陀区金沙江路 1519 弄"
          },
          {
            id: 32,
            date: "2016-05-01",
            name: "王小虎",
            address: "上海市普陀区金沙江路 1519 弄"
          }
        ]);
      }, 1000);
    },
    // 2.6 点击了编辑
    handleEdit(scope) {
      // console.log(scope);
      // index :有这条数据的下标
      // row:这行数据的内容
      // 通知父组件
      // 3.5 修改
      this.$emit("willUpdate",scope.row.id);
    },
    handleDelete(scope) {
      //3.4 点击了删除
      this.$confirm("你确定要删除吗?", "提示", {
        confirmButtonText: "删除",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          //  开始删除----发请求
          reqroledelete({
            id:scope.row.id
          }).then(res => {
            if (res.data.msg) {
              // 1.弹成功
              successAlert(res.data.msg);
              // 2.重新渲染列表
              this.$emit('init')
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>

<style>
</style>